// pages/_class/class_list/class_list.js
const appg = getApp().globalData
import Notify from '@vant/weapp/notify/notify';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    calssList: [],
    userInfo: [],
    page: {
      pageNum: 0,
      pageSize: 10,
      totalCount: 0
    },
    loading: false,
    uid:null,
    userType:null
  },
  to_add_title() {
    wx.navigateTo({
      url: '/subIndex/pages_first/_class/add_class/add_class',
    })
  },
  // 去班级群聊界面
  to_group_chat(e) {
    const id = e.currentTarget.dataset.id
    console.log(id);
    wx.navigateTo({
      url: '/pages/groupchat/groupchat/groupchat?id=' + id,
    })
  },
  // 获取班级列表
  async getClassList() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      loading: true
    })
    const uid = appg.userInfo.id;
    const userType = appg.userInfo.identity_ID
    this.data.page.pageNum += 1
    try {

      const json = await getApp().$axios("/getGroupChatList", "POST", {
        uid,
        ...this.data.page,
        userType,
        school_ID:appg.userInfo.school_ID
      });
      wx.hideLoading()
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      this.setData({
        calssList: [...this.data.calssList, ...json.data],
        page: json.page,
        userInfo: [...this.data.userInfo, ...json.userInfo],
        loading: false
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // onScrollToLower下拉触底
  onScrollToLower() {
    console.log("到底");
    if (!this.data.loading && this.data.page.pageNum * this.data.page.pageSize <= this.data.page.totalCount && (this.data.keyword == null || this.data.keyword == "")) {
      console.log("到底部了");
      this.getClassList()
    }
    if (!this.data.loading && this.data.page.pageNum * this.data.page.pageSize <= this.data.page.totalCount && (this.data.keyword !== null || this.data.keyword !== "")) {
      this.getSearchClassList()
    }
  },
  // onSearch发请求搜索
  async onSearch(e) {
    const keyword = e.detail;
    this.data.page = {
      pageNum: 0,
      pageSize: 10,
      totalCount: 0
    }
    this.setData({
      calssList: [],
      userInfo: []
    })

    // this.data.loading = true
    this.data.keyword = keyword
    console.log('检查data,', this.data);
    this.getSearchClassList()
  },
  // 搜索函数
  async getSearchClassList() {
    wx.showLoading({
      title: '搜索中',
    })
    this.setData({
      loading: true,
    })
    this.data.page.pageNum += 1
    const userType = appg.userInfo.identity_ID
    try {
      const json = await getApp().$axios("/getGroupChatListByKeyWord", "POST", {
        keyWord: this.data.keyword,
        ...this.data.page,
        uid: appg.userInfo.id,
        userType
      })
      wx.hideLoading()
      console.log([...this.data.calssList, ...json.data]);
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      };
      this.setData({
        calssList: [...this.data.calssList, ...json.data],
        page: json.page,
        userInfo: [...this.data.userInfo, ...json.userInfo],
        loading: false
      })
      console.log("最后的data,", this);
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  //清空关键字
  onCancel() {
    this.setData({
      keyword: null,
      page: {
        pageSize: 10,
        pageNum: 0,
        totalCount: 0,
      },
      calssList: [],
      userInfo: []
    })
    this.getClassList()
  },
  // 解散群
  async deleteGroup(e) {
    console.log(e);
    const id = e.currentTarget.dataset.id
    try {
      const json = await getApp().$axios("/dissolveGroupChat?id=" + id, "GET")
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      // 成功通知
      Notify({
        type: 'success',
        message: '解散成功'
      });
      this.data.page = {
        pageSize: 10,
        pageNum: 0,
        totalCount: 0
      }
      this.setData({
        page: this.data.page,
        calssList: [],
        userInfo: []
      })
      this.getClassList()
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getClassList()
    this.setData({
      uid:appg.userInfo.id,
      userType:appg.userInfo.identity_ID
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})